﻿#region

using System.Drawing;
using System.Windows.Forms;
using Tank90.Properties;

#endregion

namespace Tank90
{
    public class PlayerTank : Tank
    {
        private Image[] Images;
        private int _BulletPower;
        private int firingCount; //已经发出去的子弹数量

        public PlayerTank()
        {
        }

        public PlayerTank(int x, int y)
        {
            this.x = x;
            this.y = y;
            dirImage = directionImages[0];
            _BulletPower = 1;
            Images = directionImages;
            BulletSpeed = 10;
            IsBorned = true;
            BornTime = 0;

        }

        #region ArrayImages

        private readonly Image[] directionImages =
        {
            Resources.Player1Up,
            Resources.Player1Down,
            Resources.Player1Left,
            Resources.Player1Right
        };

        private readonly Image[] directionImagesPower =
        {
            Resources.sPlayer1Up,
            Resources.sPlayer1Down,
            Resources.sPlayer1Left,
            Resources.sPlayer1Right
        };

        private readonly Image[] directionImagesPowerPlus =
        {
            Resources.tPlayer1Up,
            Resources.tPlayer1Down,
            Resources.tPlayer1Left,
            Resources.tPlayer1Right
        };

        #endregion

        public bool IsFiring { get; set; }
        public int BulletCount { get; set; }

        public int BulletSpeed { get; set; }

        public int BulletPower
        {
            get { return _BulletPower; }
            set
            {
                _BulletPower = value;
                SetImage(value);
            }
        }

        private void SetImage(int value)
        {
            if (value == 2)
            {
                Images = directionImagesPower;
            }
            else
            {
                Images = directionImagesPowerPlus;
            }
        }

        public void Move(KeyEventArgs key)
        {
            if (!IsDied)
            {
                IsBeyond();
                
                if (key.KeyCode == Keys.Up)
                {
                    directions = Directions.Up;
                    dirImage = Images[0];
                    y -= speed;
                    BulletX = x + 22;
                    BulletY = y + 15;
                }
                if (key.KeyCode == Keys.Down)
                {
                    directions = Directions.Down;
                    dirImage = Images[1];
                    y += speed;
                    BulletX = x + 22;
                    BulletY = y + 25;
                }
                if (key.KeyCode == Keys.Left)
                {
                    directions = Directions.Left;
                    dirImage = Images[2];
                    x -= speed;
                    BulletX = x + 15;
                    BulletY = y + 22;
                }
                if (key.KeyCode == Keys.Right)
                {
                    directions = Directions.Right;
                    dirImage = Images[3];
                    x += speed;
                    BulletX = x + 20;
                    BulletY = y + 22;
                }
            }

            if (key.KeyCode == Keys.A)
            {
                if (!IsDied)
                {
                    if (!IsFiring || Bullet == null)
                    {
                        Sound.Play("fire.wav");
                        IsFiring = true;
                        SetBullet();
                        Bullet = new Bullet(BulletX, BulletY, directions);
                        Bullet.power = _BulletPower;
                        Bullet.speed = BulletSpeed;
                    }
                }
            }
        }

        public override void Drawing(Graphics graphics)
        {
            if (BornTime<48)
            {
                BornTime++;
                return;
            }
            else
            {


                if (dirImage != null)
                {
                    graphics.DrawImage(dirImage, new Point(x, y));
                }
                if (IsFiring && Bullet != null)
                {

                    Bullet.Move();
                    if (!Bullet.IsBeyondEdge())
                    {

                        Bullet.Drawing(graphics);
                    }
                    else
                    {
                        IsFiring = false;
                        Bullet = null;
                    }
                }
                else
                {
                    IsFiring = false;
                    Bullet = null;
                }
            }
        
    }


        protected override void SetBullet()
        {
            if (directions == Directions.Up)
            {
                directions = Directions.Up;

                BulletX = x + 13;
                BulletY = y + 10;
            }
            if (directions == Directions.Down)
            {
                directions = Directions.Down;


                BulletX = x + 12;
                BulletY = y + 20;
            }
            if (directions == Directions.Left)
            {
                directions = Directions.Left;
                BulletX = x + 5;
                BulletY = y + 13;
            }
            if (directions == Directions.Right)
            {
                directions = Directions.Right;
                BulletX = x + 20;
                BulletY = y + 14;
            }
        }
    }
}